package com.csthink.mysql.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csthink.mysql.entity.OpSystemConfig;
import com.csthink.mysql.mapper.OpSystemConfigMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-11
 */
@Repository
public class OpSystemConfigRepository extends ServiceImpl<OpSystemConfigMapper, OpSystemConfig> {

    @Resource
    private OpSystemConfigMapper opSystemConfigMapper;

    public List<OpSystemConfig> listByKeys(Set<String> keys) {
        if (CollectionUtils.isEmpty(keys)) {
            return null;
        }
        LambdaQueryWrapper<OpSystemConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(OpSystemConfig::getKey, keys);
        return opSystemConfigMapper.selectList(wrapper);
    }

    public List<OpSystemConfig> listByKey(String key) {
        LambdaQueryWrapper<OpSystemConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OpSystemConfig::getKey, key);
        return opSystemConfigMapper.selectList(wrapper);
    }

    public OpSystemConfig getByKey(String key) {
        LambdaQueryWrapper<OpSystemConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OpSystemConfig::getKey, key);
        wrapper.last("limit 1");
        return opSystemConfigMapper.selectOne(wrapper);
    }
}
